import { http } from '@/utils/http'
import { baseURLApi } from '../utils'
import type { resResult, pageData } from '../utils'

// 评论数据类型定义
export interface Comment {
  id: number
  articleId: number
  author: string
  authorId?: number
  content: string
  createTime: string
  updateTime?: string
  likes: number
  isLiked: boolean
  isAuthor: boolean
  status: number // 0: 正常, 1: 已删除, 2: 待审核
  replies?: Comment[]
  parentId?: number // 父评论ID，用于回复功能
}

// 评论查询参数
export interface CommentQueryParams {
  articleId: number
  page: number
  pageSize: number
  parentId?: number // 父评论ID，用于获取回复列表
}

// 评论提交参数
export interface CommentSubmitParams {
  articleId: number
  content: string
  parentId?: number // 父评论ID，用于回复功能
}

// 评论点赞参数
export interface CommentLikeParams {
  commentId: number
  action: 'like' | 'unlike'
}

export interface ReplySubmitParams {
  articleId: number
  parentId: number
  content: string
}

// 获取评论列表
export const getComments = (params: CommentQueryParams) => {
  return http.request<resResult<pageData<Comment[]>>>('get', baseURLApi('/comment/list'), {
    params
  })
}

// 提交评论
export const submitComment = (data: CommentSubmitParams) => {
  return http.request<resResult<Comment>>('post', baseURLApi('/comment/submit'), { data })
}

// 提交回复
export const submitReply = (data: ReplySubmitParams) => {
  return http.request<resResult<Comment>>('post', baseURLApi('/comment/reply'), { data })
}

// 点赞/取消点赞评论
export const likeComment = (data: CommentLikeParams) => {
  return http.request<resResult<boolean>>('post', baseURLApi('/comment/like'), { data })
}

// 删除评论
export const deleteComment = (commentId: number) => {
  return http.request<resResult<boolean>>('delete', baseURLApi(`/comment/delete/${commentId}`))
}

// 获取评论统计信息
export const getCommentStats = (articleId: number) => {
  return http.request<resResult<{ totalComments: number; totalLikes: number }>>(
    'get',
    baseURLApi('/comment/stats'),
    { params: { articleId } }
  )
}
